<template>
    <Modal
            v-model="modelShow"
            width="700"
            :title="getModalTitle"
            :mask-closable="false"
    >
        <wl-upload-oss-files
                v-model="imgPath"
                :maxNum="tableRow.num"
                uploadType="hadoop"
                :uploadUrl="getUploadUrl"
                fileName="origin"
                :maxSize="2"
                :rootDir="getRootDir"
                :beforeUploadFilter="uploadFilter"
                @result="ossFilesResultAction"
                :format="['png','jpg','jpeg']">
        </wl-upload-oss-files>
        <div slot="footer">
            <Button type="error" @click="cancelAction">取消</Button>
            <Button type="success" :loading="loading" @click="okAction">保存</Button>
        </div>
    </Modal>
</template>

<script>
import { isNumber } from '@/libs/util'

export default {
  name: 'ImportImgs',
  data () {
    return {
      modelShow: false,
      imgPath: '',
      formatData: ['jpeg', 'jpg', 'png'],
      loading: false,
      checkDir: '',
      caiLiaotitle: '',
      tableRow: {}
    }
  },
  props: {
    row: {
      type: Object,
      default: () => {
        return {}
      }
    },
    list: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  computed: {
    getUploadUrl () {
      return this.$config.upLoadFileUrl
    },
    getModalTitle () {
      return '上传【' + this.caiLiaotitle + '】图片'
    },
    getRootDir () {
      let row = this.row
      return row.company_code + '/' + row.idcard + '/' + this.tableRow.dir
    }
  },
  methods: {
    modelAction (row) {
      if (this.modelShow === false) {
        this.tableRow = row
        this.imgPath = row.img_path
        this.checkDir = row.dir
        this.caiLiaotitle = row.title
      }
      this.modelShow = !this.modelShow
    },
    okAction () {
      this.loading = true
      // if (this.imgPath !== '') {
      this.loading = false
      this.$emit('result', this.imgPath, this.tableRow)
      this.modelAction()
      // } else {
      //   this.loading = false
      // }
    },
    cancelAction () {
      this.modelShow = false
    },
    uploadFilter (file, uploadList, filterReturn) {
      let isExit = false
      if (uploadList.length > 0) {
        uploadList.forEach(item => {
          if (item.url.indexOf(file.name) !== -1) {
            isExit = true
          }
        })
      }
      if (isExit) {
        this.errMsgAction(file.name, '【文件已经存在】')
        return { errcode: false, selfShow: true }
      }
      let arr = file.name.split('.')
      let arr2 = arr[0].split('-')

      if (arr2[0] !== this.row.record_number) {
        this.errMsgAction(file.name, '请以全宗号' + this.row.record_number + '开头')
        return { errcode: false, selfShow: true }
      }
      if (arr2[1] !== this.row.tui_xiu_date) {
        this.errMsgAction(file.name, '第二位退休年度必须是' + this.row.tui_xiu_date)
        return { errcode: false, selfShow: true }
      }
      if (arr2[2] !== this.row.serial_number) {
        this.errMsgAction(file.name, '第三位的流水号必须是' + this.row.serial_number)
        return { errcode: false, selfShow: true }
      }

      if (arr[0].indexOf(this.checkDir) === -1) {
        this.errMsgAction(file.name, '图片命名必须以' + this.row.record_number + '-' + this.row.tui_xiu_date + '-' + this.row.serial_number + '-' + this.checkDir + '开头')
        return { errcode: false, selfShow: true }
      }
      let dirArr = this.checkDir.split('-')
      if (dirArr.length + 4 !== arr2.length || !isNumber(arr2[dirArr.length + 3])) {
        this.errMsgAction(file.name, '图片最后一位数量命名不对')
        return { errcode: false, selfShow: true }
      }

      return { errcode: true }
    },
    ossFilesResultAction () {
      console.log('图片处理结束')
    },
    errMsgAction (title, desc) {
      this.$Notice.warning({
        title: title,
        desc: desc,
        duration: 0
      })
    }
  }
}
</script>

<style scoped>

</style>
